package com.isp.common.utils;

import com.isp.common.model.enums.SystemCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/3/11 09:29
 */
public class WordArtUtils {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(WordArtUtils.class);

    // 使用Map存储关键字与对应方法的映射关系
    private static final Map<String, Consumer<FontParam>> methodMap = new HashMap<>();

    static {
        // 初始化方法映射关系
        methodMap.put("Alimama DaoLiTi", WordArtUtils::alimamaDaoLiTi);
        methodMap.put("QuanPixel 8px", WordArtUtils::quanPixel);
        methodMap.put("TsangerXWZ", WordArtUtils::cangEr);
        methodMap.put("Douyin Sans Bold", WordArtUtils::douYin);
        methodMap.put("寒蝉团圆圆", WordArtUtils::hanChan);
        methodMap.put("寒蝉云墨黑Regular", WordArtUtils::hanChanYunMo);
        methodMap.put("God-FangSongGBK-free", WordArtUtils::fangSong);
        methodMap.put("HuXiaoBo-NanShenTi2.0", WordArtUtils::huxiaobo);
        methodMap.put("HuanZiKaTangShouShu", WordArtUtils::huanzi);
        methodMap.put("LanaPixel", WordArtUtils::lanaPixel);
        methodMap.put("jiangxizhuokai", WordArtUtils::zhuokai);
        methodMap.put("JinzisheTongfang", WordArtUtils::jinzishe);
        methodMap.put("京華老宋体_KingHwa_OldSong", WordArtUtils::kingHwa);
        methodMap.put("Maoken Glitch Serif", WordArtUtils::maoken);
        methodMap.put("QTxiaotu", WordArtUtils::qtXiaotu);
        methodMap.put("JasonHandwriting6", WordArtUtils::jasonHandwriting);
        methodMap.put("Tiejili", WordArtUtils::tiejili);
        methodMap.put("XiaoyaPixel", WordArtUtils::xiaoya);
        methodMap.put("YouSheBiaoTiHei", WordArtUtils::youshe);
        methodMap.put("喻兵行楷", WordArtUtils::yubin);
        methodMap.put("YUN FENG JING LONG XING SHU", WordArtUtils::yunfeng);
        methodMap.put("BangFont-WanKu-Regular", WordArtUtils::wanku);
        methodMap.put("zihunbiantaoti", WordArtUtils::zihunbiantaoti);
        methodMap.put("Fusion Pixel", WordArtUtils::fusionPixel);
        methodMap.put("ChillRoundF", WordArtUtils::hanChanQuanyuan);
        methodMap.put("ChillCalligraphy", WordArtUtils::hanChanShuti);
        methodMap.put("Chill Round Gothic", WordArtUtils::hanChanYuanHei);
        methodMap.put("Alimama FangYuanTi VF", WordArtUtils::hanChanFangYuan);
        methodMap.put("HouZunSongTi", WordArtUtils::houzun);
        methodMap.put("Maru-berry Font", WordArtUtils::tangxianbing);
    }
    
    static class FontParam {
        Graphics2D g2d;
        Font font;
        String text;

        public FontParam() {
        }

        public FontParam(Graphics2D g2d, Font font, String text) {
            this.g2d = g2d;
            this.font = font;
            this.text = text;
        }

        public Graphics2D getG2d() {
            return g2d;
        }

        public void setG2d(Graphics2D g2d) {
            this.g2d = g2d;
        }

        public Font getFont() {
            return font;
        }

        public void setFont(Font font) {
            this.font = font;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }

    /**
     * 文字转艺术字图片
     *
     * @param text
     * @param padding
     * @param fontStyle
     * @param fontSize
     * @param fontName
     * @return
     */
    public static BufferedImage toImage(String text, int padding, Integer fontStyle, Integer fontSize,
                                        String fontName) {
        try {
            Font font = new Font(fontName, fontStyle, fontSize);
            AssertUtils.isTrue(StringUtils.isNotBlank(text), SystemCodeEnum.PARAM_ERROR, "请输入需要生成的艺术字文本");
            // 分割文本为多行
            String[] lines = text.split("\\n");
            // 获取单个字符的高度
            int unitHeight = getUnitHeight(font);
            // 获取最长行的宽度
            int maxWidth = getMaxLineWidth(lines, font, padding);
            // 计算整个图像的宽度和高度
            int width = maxWidth + padding * 2;
            int height = lines.length * unitHeight + padding * 2;
            //创建图片
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = bufferedImage.createGraphics();
            //消除锯齿
            antialiasing(g2d);
            // 绘制每一行
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                int lineY = i * unitHeight + padding;
                drawArtisticTextLine(g2d, line, font, padding, lineY, fontName, width);
            }
            g2d.dispose();
            return bufferedImage;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文字转图片
     *
     * @param string    文字
     * @param font      字体
     * @param wordWidth 单个文字宽度
     * @param height    高度
     */
    private static BufferedImage textToImage(String string, Font font, int wordWidth, int height, String fontName) {
        BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D tempG2 = (Graphics2D) bufferedImage.getGraphics();
        Rectangle2D r2d = font.getStringBounds(string, tempG2.getFontRenderContext());
        // 生成文字大小的ARGB图片，纠正部分艺术字高度测量误差
        BufferedImage bi = new BufferedImage((int) r2d.getWidth() / 9 * 10, (int) (r2d.getHeight() / 5 * 6),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) bi.getGraphics();
        bi = g2d.getDeviceConfiguration()
                .createCompatibleImage(bi.getWidth(), bi.getHeight(), Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = (Graphics2D) bi.getGraphics();
        g2d.setFont(font);
        FontParam param = new FontParam();
        param.setText(string);
        param.setFont(font);
        param.setG2d(g2d);
        invokeMethod(fontName, param);
        //消除锯齿
        antialiasing(g2d);
        g2d.dispose();
        bi = resizeImage(bi, wordWidth, height);
        return bi;
    }

    // 根据关键字调用带参数的方法
    public static void invokeMethod(String key, FontParam param) {
        Consumer<FontParam> method = methodMap.get(key);
        if (method != null) {
            method.accept(param);
        } else {
            LOGGER.warn("未找到与该key关联的方法");
        }
    }

    private static void alimamaDaoLiTi(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, x, y, 40.0f);
        //渐变
        handleGradientPaint(g2d, "#F93737", "#F7BE2C", 2, x, y);
        g2d.fill(sha);
    }

    private static void quanPixel(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, x, y, 40.0f);
        //渐变
        handleGradientPaint(g2d, "#F93737", "#2C64F7", 2, x, y);
        g2d.fill(sha);
    }

    private static void cangEr(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, x, y, 40.0f);
        g2d.setColor(Color.BLACK);
        g2d.fill(sha);
    }

    private static void douYin(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#000000", text, font, x, y, 40.0f);
        g2d.setColor(Color.WHITE);
        g2d.fill(sha);
    }

    private static void hanChan(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        Integer x = font.getSize() / 40;
        Integer y = font.getSize();
        // 绘制阴影
        Map.Entry<Integer, Integer> loc = handleShadow(g2d, "#2273FD", text, y / 28, x, y, 0.3f, y / 28);
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, loc.getKey(), loc.getValue(), 40.0f);
        //渐变
        handleGradientPaint(g2d, "#3773F9", "#89B6FA", 2, x, y);
        g2d.fill(sha);
    }

    private static void hanChanYunMo(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#0E8F79", "#25A991", 2, x, y);
        g2d.fill(sha);
    }

    private static void fangSong(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#5A37F9", "#2C7AF7", 2, x, y);
        g2d.fill(sha);
    }

    private static void huxiaobo(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制阴影
        Map.Entry<Integer, Integer> loc = handleShadow(g2d, "#FF3C77", text, y / 28, x, y, 0.2f, y / 28);
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(loc.getKey(), loc.getValue()));
        //渐变
        handleGradientPaint(g2d, "#FFD483", "#FF0070", 2, x, y);
        g2d.fill(sha);
    }

    private static void huanzi(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, x, y, 40.0f);
        //渐变
        handleGradientPaint(g2d, "#D637F9", "#3EFFD6", 4, x, y);
        g2d.fill(sha);
    }

    private static void lanaPixel(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#000000", "#FFFFFF", 6, x, y);
        g2d.fill(sha);
    }

    private static void zhuokai(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#00D4FF", text, font, x, y, 30.0f);
        //渐变
        handleGradientPaint(g2d, "#3837F9", "#2CF2F7", 10, x, y);
        g2d.fill(sha);
    }

    private static void jinzishe(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //颜色
        g2d.setColor(Color.BLACK);
        g2d.fill(sha);
    }

    private static void kingHwa(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制阴影
        Map.Entry<Integer, Integer> loc = handleShadow2(g2d, "#FFC83C", text, font, x, y, 0.3f, y / 50, y / 50);
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(loc.getKey(), loc.getValue()));
        //渐变
        handleGradientPaint(g2d, "#FFED00", "#FCA809", 2, x, y);
        g2d.fill(sha);
    }

    private static void maoken(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制阴影
        Map.Entry<Integer, Integer> loc = handleShadow2(g2d, "#FF18D1", text, font, x, y, 1.0f, y / 20, 0);
        // 绘制阴影
        Map.Entry<Integer, Integer> loc1 = handleShadow2(g2d, "#18FFFF", text, font, loc.getKey(), loc.getValue(), 1.0f,
                -y / 60, 0);
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(loc1.getKey(), loc1.getValue()));
        //颜色
        g2d.setColor(Color.WHITE);
        g2d.fill(sha);
    }

    private static void qtXiaotu(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
        //渐变
        handleGradientPaint(g2d, "#FF8CF4", "#F7BE2C", 2, x, y);
        g2d.fill(sha);
    }

    private static void jasonHandwriting(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
        //渐变
        handleGradientPaint(g2d, "#00B865", "#4FFF7E", 2, x, y);
        g2d.fill(sha);
    }

    private static void tiejili(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        Map.Entry<Integer, Integer> loc1 = handleInnerShadow(g2d, text, font, x, y, x / 60, y / 60);
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(loc1.getKey(), loc1.getValue()));
        //渐变
        handleGradientPaint(g2d, "#37F943", "#FFF12A", 2, x, y);
        g2d.fill(sha);
    }

    private static void xiaoya(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //颜色
        Color color = Color.decode("#1A79FF");
        g2d.setColor(color);
        g2d.fill(sha);
    }

    private static void youshe(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, x, y, 30.0f);
        //渐变
        handleGradientPaint(g2d, "#37F969", "#2CA6F7", 10, x, y);
        g2d.fill(sha);
    }

    private static void yubin(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //颜色
        g2d.setColor(Color.WHITE);
        g2d.fill(sha);
    }

    private static void yunfeng(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //颜色
        g2d.setColor(Color.BLACK);
        g2d.fill(sha);
    }

    private static void wanku(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#E037F9", "#2C8EF7", 2, x, y);
        g2d.fill(sha);
    }

    private static void zihunbiantaoti(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        // 绘制阴影
        Map.Entry<Integer, Integer> loc = handleShadow2(g2d, "#1EC9D1", text, font, x, y, 0.4f, y / 30, y / 30);
        // 绘制描边
        Shape sha = handleStroke(g2d, "#FFFFFF", text, font, loc.getKey(), loc.getValue(), 30.0f);
        //渐变
        handleGradientPaint(g2d, "#37F9C7", "#2C66F7", 2, x, y);
        g2d.fill(sha);
    }

    private static void fusionPixel(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#F93737", "#F62CF7", 2, x, y);
        g2d.fill(sha);
    }

    private static void hanChanQuanyuan(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#F96ADD", "#FFA4E5", 2, x, y);
        g2d.fill(sha);
    }

    private static void hanChanShuti(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        Map.Entry<Integer, Integer> loc = handleShadow2(g2d, "#A57223", text, font, x, y, 0.4f, y / 30, y / 30);
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(loc.getKey(), loc.getValue()));
        //渐变
        handleGradientPaint(g2d, "#CB913C", "#FFDC84", 2, x, y);
        g2d.fill(sha);
    }

    private static void hanChanYuanHei(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#F93737", "#CC2CF7", 2, x, y);
        g2d.fill(sha);
    }

    private static void hanChanFangYuan(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        //内阴影
        Map.Entry<Integer, Integer> loc = handleInnerShadow(g2d, text, font, x, y, x / 60, y / 60);
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(loc.getKey(), loc.getValue()));
        //渐变
        handleGradientPaint(g2d, "#008FA0", "#37C5BC", 2, x, y);
        g2d.fill(sha);
    }

    private static void houzun(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //颜色
        Color color = Color.decode("#D9B275");
        g2d.setColor(color);
        g2d.fill(sha);
    }

    private static void tangxianbing(FontParam fontParam) {
        Graphics2D g2d = fontParam.getG2d();
        Font font = fontParam.getFont();
        String text = fontParam.getText();
        int x = 0;
        int y = font.getSize();
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        //渐变
        handleGradientPaint(g2d, "#37D6F9", "#2CF771", 2, x, y);
        g2d.fill(sha);
    }

    /**
     * 重置图片大小
     *
     * @param oldImage 原图
     * @param w        新宽度
     * @param h        新高度
     * @return 新图片
     */
    private static BufferedImage resizeImage(BufferedImage oldImage, int w, int h) {
        BufferedImage newImage = new BufferedImage(w, h, oldImage.getType());
        Graphics2D g2d = newImage.createGraphics();
        //消除锯齿
        antialiasing(g2d);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(oldImage, 0, 0, w, h, null);
        g2d.dispose();
        return newImage;
    }

    //消除锯齿
    private static void antialiasing(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    }

    //绘制描边
    private static Shape handleStroke(Graphics2D g2d, String color, String text, Font font, Integer x, Integer y,
                                      float ratio) {
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        // 设置描边颜色
        Color outlineColor = Color.decode(color);
        g2d.setColor(outlineColor);
        // 设置描边宽度
        float strokeWidth = 1.0f * font.getSize() / ratio;
        g2d.setStroke(new BasicStroke(strokeWidth));
        // 绘制描边
        g2d.draw(sha);
        return sha;
    }

    //绘制阴影
    private static Map.Entry<Integer, Integer> handleShadow(Graphics2D g2d, String color, String text,
                                                            Integer layerCount, Integer x, Integer y, Float alpha, Integer offset) {
        // 设置透明度
        // 透明度范围：0.0（完全透明）到 1.0（完全不透明）
        // 创建 AlphaComposite 对象并应用透明度
        AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        g2d.setComposite(alphaComposite);
        int j = 0;
        Color shadowColor = Color.decode(color);
        g2d.setColor(shadowColor);
        while (j < layerCount) {
            g2d.drawString(text, x, y); // 绘制文本
            x -= offset;// 调整绘制点的横坐标值
            y -= offset;// 调整绘制点的纵坐标值
            j += offset;// 调整循环变量的值
        }
        // 恢复透明度
        g2d.setComposite(AlphaComposite.SrcOver);
        return new AbstractMap.SimpleEntry<>(x, y);
    }

    private static Map.Entry<Integer, Integer> handleShadow2(Graphics2D g2d, String color, String text, Font font,
                                                             Integer x, Integer y, Float alpha, Integer offsetX, Integer offsetY) {
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape textShape = tl.getOutline(AffineTransform.getTranslateInstance(x, y - 1));
        // 设置透明度
        // 透明度范围：0.0（完全透明）到 1.0（完全不透明）
        // 创建 AlphaComposite 对象并应用透明度
        AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        g2d.setComposite(alphaComposite);
        Color shadowColor = Color.decode(color);
        g2d.setColor(shadowColor);
        // 计算阴影轮廓
        AffineTransform shadowTransform = AffineTransform.getTranslateInstance(offsetX, offsetY);
        Shape shadowShape = shadowTransform.createTransformedShape(textShape);
        g2d.fill(shadowShape);
        // 恢复透明度
        g2d.setComposite(AlphaComposite.SrcOver);
        return new AbstractMap.SimpleEntry<>(x, y);
    }

    //绘制渐变
    private static void handleGradientPaint(Graphics2D g2d, String fromColor, String toColor, Integer ratio, Integer x,
                                            Integer y) {
        //渐变
        Color decode = Color.decode(toColor);
        Color decode1 = Color.decode(fromColor);
        //渐变
        GradientPaint gradient = new GradientPaint(x, y, decode1, x, y / ratio, decode);
        g2d.setPaint(gradient);
    }

    //绘制内阴影
    private static Map.Entry<Integer, Integer> handleInnerShadow(Graphics2D g2d, String text, Font font, Integer x,
                                                                 Integer y, Integer offsetX, Integer offsetY) {
        // 创建字形矢量
        FontRenderContext frc = g2d.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        Shape textShape = tl.getOutline(AffineTransform.getTranslateInstance(x, y));

        // 创建文字的内阴影
        Area textArea = new Area(textShape);
        g2d.setColor(Color.BLACK);

        // 通过创建一个偏移的形状，模拟内阴影效果
        AffineTransform transform = AffineTransform.getTranslateInstance(10, -10);
        Shape offsetShape = transform.createTransformedShape(textShape);
        Area offsetArea = new Area(offsetShape);

        // 取两个区域的差集，得到内阴影的部分
        offsetArea.subtract(textArea);
        g2d.fill(offsetArea);
        // 返回内阴影的部分
        return new AbstractMap.SimpleEntry<>(x, y);
    }

    private static int getUnitHeight(Font font) {
        Rectangle2D r = font.getStringBounds("A",
                new FontRenderContext(AffineTransform.getScaleInstance(1, 1), false, false));
        return (int) Math.ceil(r.getHeight());
    }

    private static int getMaxLineWidth(String[] lines, Font font, int padding) {
        int maxWidth = 0;
        for (String line : lines) {
            int lineWidth = getLineWidth(line, font, padding);
            maxWidth = Math.max(maxWidth, lineWidth);
        }
        return maxWidth;
    }

    private static int getLineWidth(String line, Font font, int padding) {
        int wordWidth = (int) Math.ceil(
                font.getStringBounds(line, new FontRenderContext(AffineTransform.getScaleInstance(1, 1), false, false))
                        .getWidth());
        return wordWidth + padding * 2;
    }

    private static void drawArtisticTextLine(Graphics2D g2d, String text, Font font, int padding, int lineY,
                                             String fontName, Integer width) {
        int wordWidth = (width - padding * 2) / text.length();
        char[] chars = text.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            String ch = String.valueOf(chars[i]);
            BufferedImage bi = textToImage(ch, font, wordWidth, font.getSize(), fontName);
            g2d.drawImage(bi, i * bi.getWidth() + padding, lineY - bi.getHeight() / 12, bi.getWidth(), bi.getHeight(),
                    null);
        }
    }
}
